var fs = require('fs');
var path = require('path');


function findWidgets(filePath, widgets) {
    files = fs.readdirSync(filePath);
    files.forEach(function(filename) {
        var filedir = path.join(filePath, filename);
        var stats = fs.statSync(filedir);
        var isFile = stats.isFile();
        var isDir = stats.isDirectory();
        if (isFile && filename == "widget.json") {
            var data = JSON.parse(fs.readFileSync(filedir));
            widgets.push(data)
        }
        if (isDir) {
            findWidgets(filedir, widgets);
        }
    });
    if (filePath == root) {
        console.log(widgets)
    }
}


exports.setMockups = function(app) {
    app.get("/api/widgets", function(req, res) {
        var file = "mockdata.json";
        var widgets = [];
        findWidgets('../client/src/widgets', widgets, res);
        var mockdata = { 'widgets': widgets }
        res.send(mockdata);
    })
};